package com.hexu.event.dao;

import com.hexu.event.entity.Article;
import com.hexu.event.util.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ArticleDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    //增加文章
    public boolean addArticle(Article article){
        String sql = "insert into article(name,content,collect_number,thumb_number,comment_number,specie,author," +
                "browse_number,create_time)"
                + " values (?,?,?,?,?,?,?,?,?)";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1, article.getName());
            ps.setString(2, article.getContent());
            ps.setInt(3,article.getCollectNumber());
            ps.setInt(4,article.getThumbNumber());
            ps.setInt(5,article.getCommentNumber());
            ps.setString(6,article.getSpecie());
            ps.setString(7,article.getAuthor());
            ps.setInt(8,article.getBrowseNumber());
            ps.setString(9,article.getCreateTime());
            ps.executeUpdate();

            //添加成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }
        //添加失败
        return false;
    }

    //检索是否存在一样的文章名
    public boolean existArticleName(String articleName){
        String sql = "SELECT name FROM article where name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, articleName);
            rs = ps.executeQuery();

            while (rs.next()) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, rs);
        }

        return true;
    }

    //文章浏览次数+1
    public boolean addBrowseNumber(String articleName){
        String sql = "update article set browse_number=browse_number+1 where name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, articleName);
            ps.executeUpdate();

            //修改成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //修改失败
        return false;
    }

    //文章点赞次数+1
    public boolean addThumbNumber(String articleName){
        String sql = "update article set thumb_number=thumb_number+1 where name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, articleName);
            ps.executeUpdate();

            //修改成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //修改失败
        return false;
    }
    //文章收藏次数+1
    public boolean addCollectNumber(String articleName){
        String sql = "update article set collect_number=collect_number+1 where name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, articleName);
            ps.executeUpdate();

            //修改成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //修改失败
        return false;
    }

    //文章评论次数+1
    public boolean addCommentNumber(String articleName){
        String sql = "update article set comment_number=comment_number+1 where name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, articleName);
            ps.executeUpdate();

            //修改成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //修改失败
        return false;
    }

    //返回文章浏览数
    public int getBrowseNumber(String articleName){
        String sql = "select browse_number from article where name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,articleName);
            rs = ps.executeQuery();

            while (rs.next()){
                int browseNumber = rs.getInt("browse_number");
                return browseNumber;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return 0;
    }
    //返回文章评论数
    public int getCommentNumber(String articleName){
        String sql = "select comment_number from article where name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,articleName);
            rs = ps.executeQuery();

            while (rs.next()){
                int commentNumber = rs.getInt("comment_number");
                return commentNumber;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return 0;
    }
    //返回文章点赞数
    public int getThumbNumber(String articleName){
        String sql = "select thumb_number from article where name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,articleName);
            rs = ps.executeQuery();

            while (rs.next()){
                int thumbNumber = rs.getInt("thumb_number");
                return thumbNumber;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return 0;
    }
    //返回文章收藏数
    public int getCollectNumber(String articleName){
        String sql = "select collect_number from article where name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,articleName);
            rs = ps.executeQuery();

            while (rs.next()){
                int collectNumber = rs.getInt("collect_number");
                return collectNumber;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return 0;
    }

    //删除文章
    public boolean deleteArticle(String articleName){
        String sql="delete from article where name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,articleName);
            if(ps.executeUpdate()>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }
        return false;
    }



}
